package model.trivial_pursuit;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;

import model.Params;

import database.IDbConnector;
import database.MySQLConnector;

/**
 * Classe récupérant la liste des thèmes disponibles pour le Trivial Pursuit
 * @author Aubry Pirian
 *
 */
public class ListThemes {
	
	protected IDbConnector idbc;					// l'interface d'accès
	
	public ListThemes() throws ClassNotFoundException, SQLException, NoSuchFieldException {
		if (Params.DB_TYPE.equals("mysql")) {
			try {
				this.idbc = new MySQLConnector(Params.DB_SERVER,Params.DB_USER,Params.DB_PASSWD,Params.DB_NAME);
			} catch (NoSuchFieldException e) {
				throw e;
			}
		} else {
			this.idbc=null;
			throw new NoSuchFieldException("SGBD non géré.");
		}
		try {
			this.connect();
		} catch (SQLException e) {
			throw e;
		} catch (ClassNotFoundException e) {
			throw e;
		}
	}
	
	/**
	 * Méthode créant une nouvelle connexion à la base de données spécifiée
	 * @throws ClassNotFoundException : impossible d'accéder au driver JDBC
	 * @throws SQLException 
	 */
	public void connect() throws ClassNotFoundException, SQLException {
		try {
			this.idbc.connect();
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		}
	}
	
	/**
	 * Méthode retournant la liste des themes enregistrées dans la base de données
	 * @throws SQLException 
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws ClassNotFoundException : impossible d'accéder au driver JDBC
	 */
	public LinkedList<Theme> readThemes () throws SQLException, NoSuchFieldException, ClassNotFoundException {
		LinkedList<Theme> themes = new LinkedList<Theme>();
		ResultSet rs;
		try {
			rs = this.idbc.select(TPParams.THEME_TABLE);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		try {
			while (rs.next()) {
				try {
					themes.add( new Theme(rs.getInt("id")));
				} catch (SQLException e) {
					throw e;
				} catch (ClassNotFoundException e) {
					throw e;
				}
			}
		} catch (SQLException e) {
			throw e;
		}
		return themes;
	}
	
	/**
	 * toProtocolString
	 * @throws SQLException 
	 * @throws NoSuchFieldException 
	 * @throws ClassNotFoundException 
	 */
	public String toProtocolString() throws SQLException, NoSuchFieldException, ClassNotFoundException {
		String s = "";
		s += "themes: ";
		try {
			LinkedList<Theme> lt = this.readThemes();
			Iterator<Theme> iterator = lt.iterator();
			while (iterator.hasNext()) {
				s += "[";
				Theme t = iterator.next();
				s += t.getId()+":"+t.getName();
				s += "],";
			}
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		} catch (ClassNotFoundException e) {
			throw e;
		}
		return s;
	}
	
	public static void main (String[] argv) {
			ListThemes ls;
			try {
				ls = new ListThemes();
				ls.connect();
				System.out.println(ls.toProtocolString());
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
			/*LinkedList<String> themes = ls.readThemes();
			System.out.println(themes);*/ 
	}

}
